//
//  ViewController.m
//  练习项目
//
//  Created by 吴丹草 on 17/1/4.
//  Copyright © 2017年 吴丹草. All rights reserved.
//

#import "ViewController.h"
#import "LWChannelModel.h"
#import "LWChannelView.h"
#import "LWNewsController.h"
#import "Masonry.h"


@interface ViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,LWChannelViewDelegate>

@property(nonatomic,strong)NSMutableArray *titleList;
@property(nonatomic,strong)LWChannelView *channelView;
@property(nonatomic,strong)UIPageViewController *pageVC;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _titleList = [NSMutableArray array];
    self.view.backgroundColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadData];
    [self setupUI];
}

- (void)loadData
{
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"topic_news.json" withExtension:nil];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSArray *arrDic = dic[@"tList"];
    
    [arrDic enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        LWChannelModel *model = [LWChannelModel new];
        [model setValuesForKeysWithDictionary:obj];
        [_titleList addObject:model];
    }];
    
    _titleList = [_titleList sortedArrayUsingComparator:^NSComparisonResult(LWChannelModel * obj1, LWChannelModel *obj2) {
        
        
        return [obj1.tid compare:obj2.tid];
        
    }].copy;
}

- (void)setupUI
{
    LWChannelView *channelView = [[LWChannelView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
    
    [self.view addSubview:channelView];

    channelView.titleList = _titleList.copy;
    _channelView = channelView;
    channelView.delegate = self;
    
    UIPageViewController *pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    
    pageVC.dataSource = self;
    pageVC.delegate = self;
    _pageVC = pageVC;
    
    [self addChildViewController:pageVC];
    [self.view addSubview:pageVC.view];
    [pageVC didMoveToParentViewController:self];
    
    LWNewsController *newsVC = [LWNewsController new];
    
    [pageVC setViewControllers:@[newsVC] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    [pageVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(channelView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
        
    }];
    
}

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(LWNewsController *)viewController
{
    NSInteger index = viewController.index;
    if(index == 0)
    {
        return nil;
    }
    
    LWChannelModel *model = _titleList[index];
    
    return [[LWNewsController alloc]initControllerWith:(index - 1 )withModel:model];
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(LWNewsController *)viewController
{
    
    NSInteger index = viewController.index;
    if(index == _titleList.count - 1)
    {
        return nil;
    }
       LWChannelModel *model = _titleList[index];
    return [[LWNewsController alloc]initControllerWith:(index + 1)withModel:model];
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
{
    LWNewsController *nextVC = (LWNewsController *)pendingViewControllers[0];
    [_channelView setCurrentIndex:nextVC.index];
    
//    NSLog(@"%zd",nextVC.index);
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    LWNewsController *lastVC = (LWNewsController *)pageViewController.viewControllers[0];
    [_channelView setCurrentIndex:lastVC.index];
    
//    NSLog(@"%zd",lastVC.index);
}

- (void)channelView:(LWChannelView *)channelView sendWithIndex:(NSInteger)index
{
    LWChannelModel *model = _titleList[index];
    
    LWNewsController *newsVC = [[LWNewsController alloc]initControllerWith:index withModel:model];
    
    LWNewsController *currentVC = _pageVC.viewControllers[0];
    NSInteger currentIndex = currentVC.index;
    
    UIPageViewControllerNavigationDirection direction;
    
    if(index > currentIndex)
    {
        direction = UIPageViewControllerNavigationDirectionForward;
    }
    else
    {
        direction = UIPageViewControllerNavigationDirectionReverse;
    }
    [_pageVC setViewControllers:@[newsVC] direction:direction animated:YES completion:nil];
    
     NSLog(@"%zd==%zd",index,currentIndex);
}

@end
